package tuwien.whattowear;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import tuwien.similaritymeasurement.ImageAnalyzer;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import tuwien.whattowear.R;

public class PlayActivity extends Activity {

	/** Called when the activity is first created. 
	 * 
	 * ORIENTATION CHANGE UNHANDELD
	 *
	 * */

	public boolean TESTING = true;

	private Button takeAphotoBtn;
	private Button getAdviceBtn;
	private int CAMERA_REQUEST = 1888;
	private ImageView imageView;
	private String imagePath;
	private Bitmap photo;
	
	private TextView firstText;
	private TextView secondText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.play_layout);

		takeAphotoBtn = (Button)findViewById(R.id.buttonTakeAPhoto);
		firstText = (TextView)findViewById(R.id.firstText);
		addButtonListeners();
	}


	private void addButtonListeners(){
		takeAphotoBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0){

				Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
				imagePath = Environment.getExternalStorageDirectory().getAbsolutePath();
				imagePath += "/myPicture.jpg";
				File file = new File( imagePath );
				Uri outputFileUri = Uri.fromFile( file );
				cameraIntent.putExtra("output", outputFileUri);
				startActivityForResult(cameraIntent, CAMERA_REQUEST); 

			}
		});

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
		if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
			photo = BitmapFactory.decodeFile(imagePath);
			Log.i("info", "PHOTO IS FIRST "+photo);
			if(photo!=null){
				new PostTask().execute();
			}

		}  
	}

	private class PostTask extends AsyncTask<String, Integer, String> {

		ProgressDialog dialog = new ProgressDialog(PlayActivity.this);

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog.setMessage("Analyzing the image...");
			dialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			//String url=params[0]; 
			
			Bitmap bmp = Bitmap.createScaledBitmap(photo, 519, 389, false);
			Log.i("test", "HEIGHT : "+bmp.getHeight());
		
			ImageAnalyzer ia = new ImageAnalyzer(bmp);
			
			Bitmap cloudy0 = BitmapFactory.decodeResource(getResources(),R.drawable.cloudy0);
			Bitmap cloudy1 = BitmapFactory.decodeResource(getResources(),R.drawable.cloudy1);
			Bitmap cloudy2 = BitmapFactory.decodeResource(getResources(),R.drawable.cloudy2);
			Bitmap cloudy3 = BitmapFactory.decodeResource(getResources(),R.drawable.cloudy3);
			
			Bitmap sunny0 = BitmapFactory.decodeResource(getResources(),R.drawable.sunny0);
			Bitmap sunny1 = BitmapFactory.decodeResource(getResources(),R.drawable.sunny1);
			Bitmap sunny2 = BitmapFactory.decodeResource(getResources(),R.drawable.sunny2);
			Bitmap sunny3 = BitmapFactory.decodeResource(getResources(),R.drawable.sunny3);
			
			Bitmap snowy0 = BitmapFactory.decodeResource(getResources(),R.drawable.snowy0);
			Bitmap snowy1 = BitmapFactory.decodeResource(getResources(),R.drawable.snowy1);
			Bitmap snowy2 = BitmapFactory.decodeResource(getResources(),R.drawable.snowy2);
			Bitmap snowy3 = BitmapFactory.decodeResource(getResources(),R.drawable.snowy3);
			
			ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
			bitmaps.add(cloudy0);
			bitmaps.add(cloudy1);
			bitmaps.add(cloudy2);
			bitmaps.add(cloudy3);
			
			bitmaps.add(sunny0);
			bitmaps.add(sunny1);
			bitmaps.add(sunny2);
			bitmaps.add(sunny3);
			
			bitmaps.add(snowy0);
			bitmaps.add(snowy1);
			bitmaps.add(snowy2);
			bitmaps.add(snowy3);
			
			double distance = 2000;
			Bitmap closest = null;
			int closestResult = 0;
			for (int i = 0; i < bitmaps.size(); i++) {
				double tempDistance = ia.calcDistance(bitmaps.get(i));
				Log.i("test", "DISTANCE from PHOTO " +i+ " is: " + tempDistance);
				if(tempDistance<distance){
					closest = bitmaps.get(i);
					closestResult = i;
					distance = tempDistance;
				}
			}
			
			String result = "";
			
			Log.i("test", "Closest result photo nr.: "+closestResult);
			
			if(closest!=null){
				
				int closestPhoto = closestResult % 4;
				Log.i("test", "Closest image: "+ closestPhoto);
				
				if(closestResult<4) result = "cloudy"+closestPhoto;
				else if (closestResult>3 && closestResult<8) result = "sunny"+closestPhoto;
				else result = "snowy"+closestPhoto;
			}
			
			return result;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			//updateProgressBar(values[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			Log.i("info", "FINISHED ANALYZING");
			dialog.hide();
			Intent in = new Intent(PlayActivity.this, AdviceActivity.class);
			in.putExtra("analysis_result", result);
			startActivity(in);

		}
	}


}